package org.colswedev.app.labhudson.dao;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.colswedev.app.labhudson.entity.Customer;

/**
 *
 * @author Leonardo Bohórquez
 */
public class CustomerDAO implements DAO<Customer> {

    private EntityManagerFactory emf;
    private EntityManager em;

    public CustomerDAO(EntityManagerFactory emf) {
	this.emf = emf;
    }

    @Override
    public boolean create(Customer toCreate) {
	boolean result = true;
	try {
	    em = emf.createEntityManager();
	    em.getTransaction().begin();
	    em.persist(toCreate);
	    em.getTransaction().commit();
	} catch (Exception e) {
	    result = false;
//	    System.err.print(e.getMessage());
//	    e.printStackTrace(System.err);
	    System.err.println("No se pudo crear el registro");
	} finally {
	    if (em != null && em.isOpen()) {
		em.close();
	    }
	}
	return result;
    }

    @Override
    public Customer read(Customer toRead) {
	Customer customer;
	try {
	    em = emf.createEntityManager();
	    Query q = em.createQuery("SELECT c FROM Customer AS c WHERE c.userName=:customerParam");
	    q.setParameter("customerParam", toRead.getUserName());
	    em.getTransaction().begin();
	    customer = (Customer) q.getSingleResult();
	    em.getTransaction().commit();
	} catch (NoResultException e) {
//	    System.err.print(e.getMessage());
//	    e.printStackTrace(System.err);
	    System.err.println("No existe el registro");
	    customer = null;
	} finally {
	    if (em != null && em.isOpen()) {
		em.close();
	    }
	}
	return customer;
    }

    @Override
    public boolean delete(Customer toDelete) {
	boolean result = true;
	try {
	    if ((toDelete = read(toDelete)) != null) {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		toDelete = em.getReference(Customer.class, toDelete.getId());
		em.remove(toDelete);
		em.getTransaction().commit();
	    } else {
		throw new Exception("The registry does not exist");
	    }
	} catch (Exception ex) {
	    System.err.println(ex.getMessage());
	    ex.printStackTrace(System.err);
	    if (em.isOpen()) {
		em.getTransaction().rollback();
	    }
	    result = false;
	} finally {
	    if (em != null && em.isOpen()) {
		em.close();
	    }
	}
	return result;
    }

    @Override
    public boolean update(Customer toUpdate) {
	boolean result = true;
	Customer old;
	try {
	    if ((old = read(toUpdate)) != null) {
		toUpdate.setId(old.getId());
		em = emf.createEntityManager();
		em.getTransaction().begin();
		em.merge(toUpdate);
		em.getTransaction().commit();
	    } else {
		throw new Exception("The registry does not exist");
	    }
	} catch (Exception ex) {
	    System.err.println(ex.getMessage());
	    ex.printStackTrace(System.err);
	    if (em.isOpen()) {
		em.getTransaction().rollback();
	    }
	    result = false;
	} finally {
	    if (em != null && em.isOpen()) {
		em.close();
	    }
	}
	return result;
    }

    @Override
    public Collection<Customer> readAll() {
	Collection<Customer> customers=null;
	try {
	    em = emf.createEntityManager();
	    Query query = em.createQuery("SELECT c FROM Customer AS c");
	    em.getTransaction().begin();
	    customers = query.getResultList();
	    em.getTransaction().commit();
	} catch (Exception ex) {
//	    System.err.println(ex.getMessage());
	    System.err.println("No hay registros de clientes");
	} finally {
	    if (em != null && em.isOpen()) {
		em.close();
	    }
	}
	return customers;
    }
}
